package cn.hahah.module.blog.service.userdialoguepersonal;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userdialoguepersonal.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialoguepersonal.UserDialoguePersonalDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userdialoguepersonal.UserDialoguePersonalMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户对话个人 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserDialoguePersonalServiceImpl implements UserDialoguePersonalService {

    @Resource
    private UserDialoguePersonalMapper userDialoguePersonalMapper;

    @Override
    public Long createUserDialoguePersonal(UserDialoguePersonalSaveReqVO createReqVO) {
        // 插入
        UserDialoguePersonalDO userDialoguePersonal = BeanUtils.toBean(createReqVO, UserDialoguePersonalDO.class);
        userDialoguePersonalMapper.insert(userDialoguePersonal);
        // 返回
        return userDialoguePersonal.getId();
    }

    @Override
    public void updateUserDialoguePersonal(UserDialoguePersonalSaveReqVO updateReqVO) {
        // 校验存在
        validateUserDialoguePersonalExists(updateReqVO.getId());
        // 更新
        UserDialoguePersonalDO updateObj = BeanUtils.toBean(updateReqVO, UserDialoguePersonalDO.class);
        userDialoguePersonalMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserDialoguePersonal(Long id) {
        // 校验存在
        validateUserDialoguePersonalExists(id);
        // 删除
        userDialoguePersonalMapper.deleteById(id);
    }

    private void validateUserDialoguePersonalExists(Long id) {
        if (userDialoguePersonalMapper.selectById(id) == null) {
            throw exception(USER_DIALOGUE_PERSONAL_NOT_EXISTS);
        }
    }

    @Override
    public UserDialoguePersonalDO getUserDialoguePersonal(Long id) {
        return userDialoguePersonalMapper.selectById(id);
    }

    @Override
    public PageResult<UserDialoguePersonalDO> getUserDialoguePersonalPage(UserDialoguePersonalPageReqVO pageReqVO) {
        return userDialoguePersonalMapper.selectPage(pageReqVO);
    }

}